<?php
declare (strict_types = 1);

namespace app\middleware;

use app\common\model\Order as OrderModel;

class CountTime
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        //没支付并过去30分钟的订单，变成交易关闭
        $OrderModel = new OrderModel();

        $no_orders_arr = $OrderModel
            ->where('pay_status','=',1)
            ->where('add_time','<',time()- 1800 )
            ->column("id");

        $no_orders_str = implode(',',$no_orders_arr);

        $OrderModel->where('id','in',$no_orders_str)->update([
            'pay_status'=>2,
            'un_time'=>time()
        ]);

        $response = $next($request);
        return $response;
    }
}
